package 算法.base;

import org.junit.Test;

/**
 * @author lchenglong
 * @date 2022/2/11
 */
public class ReferTest {
     ThreadLocal<Integer> local = new ThreadLocal<Integer>();
     Integer value = null;
     class ThreadA extends Thread{
        public void run(){
            local.set(1);
            value = local.get();
            System.out.println("设置后：" +value);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            value = local.get();
            System.out.println("回收后：" +value);
        }
    }

     class ThreadB extends Thread {
        public void run(){
            System.gc();
            System.out.println("垃圾回收。。。");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }


    @Test
    public void test() throws InterruptedException {
//        local.set(1);
        ThreadA threadA = new ThreadA();
        threadA.start();
        Thread.sleep(10);
        ThreadB threadB = new ThreadB();
        threadB.start();
//        Thread.sleep(10);
//        local.set(1);
//        System.gc();
//        Thread.sleep(10);
//        Integer value = local.get();

        Thread.sleep(2000);

    }


}
